// pages/login/login.js
import util from '../../../utils/util'
import Dialog from 'vant-weapp/dialog/dialog';
const app = getApp();
const http = app.http;
const userInfoManager = app.userInfoManager;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    color: '#000000',
    background: '#ffffff',
    title: "修改密码",
    changePwd: "",
    fromLogin: "",
    downtime: 60, //倒计时时间
    hasClickCode: false, //是否可以点击获取验证码按钮
    codeText: "获取验证码", //验证码按钮文字
    loginEnable: false, //登录按钮是否可以按钮
    phoneValue: '',
    pwdValue: '',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.data.changePwd = options.changePwd;
    this.data.fromLogin = options.fromLogin;
    console.log(this.data.changePwd, this.data.fromLogin);
    console.log(this.data.changePwd === 'true', this.data.fromLogin === 'true');
    if (this.data.changePwd === 'true' && this.data.fromLogin === 'true') {
      this.setData({
        title: "修改密码"
      });
      wx.showToast({
        duration: 1000,
        icon: 'none',
        title: "该账号首次登录系统，请修改密码"
      })
    } else if (this.data.changePwd === 'false' && this.data.fromLogin === 'true') {
      this.setData({
        title: "忘记密码"
      });
    } else {
      this.setData({
        title: "修改密码"
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 手机号输入监听
   * @param {} e 
   */
  onPhoneInputListener(e) {
    let phoneValue = util.validateNumber(e.detail.value)
    this.setData({
      phoneValue
    })
    this.canClickLogin();
    this.canClickCode();
  },
  onPwdInputListener(e) {
    // let pwdValue = util.validateNumber(e.detail.value)
    this.setData({
      pwdValue: e.detail.value
    })
    this.canClickLogin();
  },
  /**
   * 获取验证码
   */
  clickCode(e) {
    if (!this.data.hasClickCode) return;
    let mesText = '是否向 ' + this.data.phoneValue + ' 发送验证码';
    Dialog.confirm({
      title: '确认手机号码',
      message: mesText,
      confirmButtonColor: "#21af65",
    }).then(() => {
      this.setData({
        hasClickCode: false
      });
      this.getCountDown();
      http.getCaptcha(this.data.phoneValue).then(res => {
        if (res.code == 0) {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: '发送成功'
          })
        } else {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: res.msg
          })
        }
      })
    }).catch(() => {

    });
  },
  getCountDown() { //验证码一分钟倒计时
    var time = parseInt(this.data.downtime);
    if (time <= 0) {
      this.setData({
        codeText: "获取验证码"
      }, function() {
        this.canClickCode();
      });
      this.data.downtime = 60;
      return false;
    }
    this.data.downtime = parseInt(this.data.downtime) - 1;
    this.setData({
      codeText: this.data.downtime + "秒后重试"
    })
    setTimeout(this.getCountDown, 1000);
  },
  /**
   * 点击下一步
   * @param {*} e 
   */
  nextStep(e) {
    if (!this.data.loginEnable) return;
    http.checkCaptcha(this.data.phoneValue, this.data.pwdValue).then(res => {
      if (res.code == 0) {
        wx.redirectTo({
          url: '/pages/login-process/forgetpassword/newpassword?phone=' + this.data.phoneValue + '&captcha=' + this.data.pwdValue + '&changePwd=' + this.data.changePwd + '&fromLogin=' + this.data.fromLogin,
          success: (result) => {},
          fail: () => {},
          complete: () => {}
        });
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })

  },
  /**
   * 登录按钮是否可以点击
   */
  canClickLogin() {
    const b = this.data.phoneValue && this.data.phoneValue.length === 11 && this.data.pwdValue && this.data.pwdValue.length >= 4;
    this.setData({
      loginEnable: b
    })
  },
  /**
   * 验证码按钮是否可以点击
   */

  canClickCode() {
    const b = this.data.phoneValue && this.data.phoneValue.length === 11 && this.data.codeText === "获取验证码";
    this.setData({
      hasClickCode: b
    })
  }
})